﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Evolution
{
    class Evol
    {
        byte[,] State = new byte[32, 32]; //field data
        public Evol()
        {
            Random rand = new Random();
            for (int i = 0; i < 32; i++)  //create a frame with impossible values
            {
                State[0, i] = 2;
                State[i, 0] = 2;
                State[i, 31] = 2;
                State[31, i] = 2;
            }
            for (int i = 1; i < 31; i++)   
                for (int j = 1; j < 31; j++)
                    State[i, j] = (byte)rand.Next(2);        
        }
        public void Draw()
        {//display matrix
            for (int i = 1; i < 31; i++)
            {
                for (int j = 1; j < 31; j++)
                    if (State[i, j] == 1) Console.Write("*"); //'*' if cell is alive 
                    else Console.Write(" ");//' ' if cell id free
                Console.WriteLine();
            }
        }
        public void Step()
        {//main function, wich make a step of evolution and change our matrix
            byte zeroc=0,onec=0;
            byte[,] buf = new byte[32, 32];
            Array.Copy(State, buf, buf.Length);
            for (int i = 1; i < 31; i++)
                for (int j = 1; j < 31; j++)
                {
                    Envir(i, j, ref zeroc, ref onec);
                    if ((State[i, j] == 1) && (zeroc == 0)) buf[i, j] = 0;
                    else if ((State[i, j] == 0) && (onec >= 2)) buf[i, j] = 1;
                }
            Array.Copy(buf, State, State.Length);
        }
        protected void Envir(int i,int j, ref byte zeroc,ref byte onec )
        {//determines which cells surround the current
            zeroc=0;
            onec=0;
            for (int k = i - 1; k <= i + 1; k++)
                for (int x = j - 1; x <= j+1; x++)
                    if ((k!=i)||(x!=j))
                    {
                        if (State[k, x] == 1) onec ++; //calculates cell with 1
                        else if (State[k, x] == 0) zeroc++;//calculates cell with 0
                    }
        }    
    }    
    class Program
    {
        static void Main(string[] args)
        {
            Evol evo = new Evol();
            Console.Title = "Evolution";
            Console.WindowHeight=34;
            Console.WriteLine("Press Enter to start Evolution.");
            Console.ReadLine();
            Console.Clear();
            evo.Draw();
            Console.WriteLine("Press spacebar to continue or any other key to stop");
            while (Console.ReadKey().KeyChar == ' ')
            {
                Console.Clear();
                evo.Step();
                evo.Draw();
                Console.WriteLine("Press spacebar to continue or any other key to stop");
            }
            Console.Clear();
            evo.Draw();
            Console.WriteLine("End of evolution");
            Console.ReadLine();
        }
    }
}
